package com.sssoft.Yundian.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sssoft.Yundian.R;
import com.sssoft.Yundian.Dao.GoodsDao;
import com.sssoft.Yundian.Dao.DataDao;
import com.sssoft.Yundian.bean.Other;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
public class AddGoodsActivity extends Activity {
	private ImageView image;
	private EditText et_goodname;
	private EditText et_goodspecification;
	private EditText et_goodvalue;
	private EditText et_goodnumber;
	private EditText et_goodnote;
	private Button button1;
	private GoodsDao dao;
	private String[] a;
	private String[] b;
	private ArrayAdapter<String> adapter1;
	private ArrayAdapter<String> adapter2;

	private Button button2;
	private Button button3;
	private Button button4;
	private Spinner spinner1;
	private Spinner spinner2;
	public static final int NONE = 0;    
    public static final int PHOTOHRAPH = 1;// 拍照    
    public static final int PHOTOZOOM = 2; // 缩放    
    public static final int PHOTORESOULT = 3;// 结果 
    
    public static final String IMAGE_UNSPECIFIED = "image/*";    
    ImageView imageView = null;    
    Button btc= null;    
    Button btl = null; 

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addgoods);
		
		//获取界面布局中的spinner组件
		spinner1=(Spinner) findViewById(R.id.type);
		dao = new GoodsDao(this);
		a = dao.queryAllType();

		//创建Array适配器对象
		adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,a);
		//为spinner设置adapter
		spinner1.setAdapter(adapter1);
		
		spinner2=(Spinner) findViewById(R.id.unit);
		b = dao.queryAllunits();

		//创建Array适配器对象
		adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,b);
		//为spinner设置adapter
		spinner2.setAdapter(adapter2);
		
		
		
		
		// 存数据
		et_goodname = (EditText) findViewById(R.id.et_goodname);
		et_goodspecification = (EditText) findViewById(R.id.et_goodspecification);
		et_goodvalue = (EditText) findViewById(R.id.et_goodvalue);
		et_goodnumber = (EditText) findViewById(R.id.et_goodnumber);
		et_goodnote = (EditText) findViewById(R.id.et_goodnote);

		button2 = (Button) findViewById(R.id.bt_Menu);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				
			}
		});

		button3 = (Button) findViewById(R.id.bt_add1);
		button3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AddGoodsActivity.this, AddtypeActivity.class);
				startActivity(intent);
				finish();
				
			}
		});

		button4 = (Button) findViewById(R.id.bt_add2);
		button4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AddGoodsActivity.this, AddunitsActivity.class);
				startActivity(intent);
				finish();
				
				
			}
		});
		

		Button button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new Button.OnClickListener() {
			// 信息转换成字符串形式
			public void onClick(View view) {
				String name = et_goodname.getText().toString();
				String specification = et_goodspecification.getText().toString();
				String value = et_goodvalue.getText().toString();
				String number = et_goodnumber.getText().toString();
				String note = et_goodnote.getText().toString();

				if (dao.register(name, specification, value, number, note)) {
					Toast.makeText(AddGoodsActivity.this, "保存成功!", Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(AddGoodsActivity.this, LoginActivity.class);
					startActivity(intent);
					finish();
				}
			}
		});
		
		
		//相机本地图片
	    imageView = (ImageView) findViewById(R.id.iv_1);    
        btc = (Button) findViewById(R.id.buttonCamera);    
        btl = (Button) findViewById(R.id.buttonLocal);
        btl.setOnClickListener(new Button.OnClickListener() {
        	@Override 	//打开本地图片选择本地图片
        	public void onClick(View view) {
        		Intent intent = new Intent(Intent.ACTION_PICK, null);    
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); 
                startActivityForResult(intent, PHOTOZOOM);
			}
	});
        //打开照相机拍照
        btc.setOnClickListener(new Button.OnClickListener() {
        	@Override
        	 public void onClick(View v) {    
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));    
                startActivityForResult(intent, PHOTOHRAPH);    
            }    
        });
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 if (resultCode == NONE)    
	            return;
		// 拍照    
	        if (requestCode == PHOTOHRAPH) {
	        	//设置文件保存路径这里放在跟目录下    
	            File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");    
	            startPhotoZoom(Uri.fromFile(picture));
	        }
	        if (data == null)    
	            return;    
	            
	        // 读取相册缩放图片    
	        if (requestCode == PHOTOZOOM) {    
	            startPhotoZoom(data.getData());    
	        }
	     // 处理结果    
	        if (requestCode == PHOTORESOULT) {    
	            Bundle extras = data.getExtras();    
	            if (extras != null) {    
	                Bitmap photo = extras.getParcelable("data");    
	                ByteArrayOutputStream stream = new ByteArrayOutputStream();    
	                photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件  
	                
	                imageView.setImageBitmap(photo);    
	            }    
	    
	        }
	        super.onActivityResult(requestCode, resultCode, data);    
    }    
	        
	public void startPhotoZoom(Uri uri) {    
        Intent intent = new Intent("com.android.camera.action.CROP");    
        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);    
        intent.putExtra("crop", "true");    
        // aspectX aspectY 是宽高的比例    
        intent.putExtra("aspectX", 1);    
        intent.putExtra("aspectY", 1);    
        // outputX outputY 是裁剪图片宽高    
        intent.putExtra("outputX", 200);    
        intent.putExtra("outputY", 200);    
        intent.putExtra("return-data", true);    
        startActivityForResult(intent, PHOTORESOULT);    
    } 
	
}